var app = getApp();

Page({
    data: {
        nav: [],
        navCur: 0,
        acpackage: [],
        piece: 0,
        popout_hide: !0,
        detNav: [ "图文详情", "商品参数" ],
        detCur: 0,
        goodsinfo: [],
        goods_hide: !0,
        pitch: [],
        selectHid: !0,
        length: 0
    },
    click_det: function(t) {
        console.log(t), this.setData({
            detCur: t.currentTarget.dataset.id
        });
    },
    click_nav: function(t) {
        var e = t.currentTarget.id, a = this.data.nav[e].id, o = this;
        app.util.request({
            url: "entry/wxapp/package",
            showLoading: !1,
            data: {
                op: "packinfo",
                packageid: a
            },
            success: function(t) {
                var a = t.data.data;
                o.setData({
                    acpackage: a,
                    chosenum: o.data.nav[e].chosenum,
                    pitch: [],
                    piece: 0
                });
            }
        }), this.setData({
            navCur: t.currentTarget.id
        });
    },
    click_off: function() {
        this.setData({
            popout_hide: !this.data.popout_hide
        });
    },
    choose_pro: function(t) {
        var a = t.currentTarget.dataset.index, e = t.currentTarget.dataset.attr, o = this.data.acpackage, s = this.data.chosenum;
        if (0 == s) wx.showToast({
            title: "无兑换次数，请先参与活动~",
            icon: "none"
        }); else {
            o.contents[a].attrs[e] = {
                proCur: !o.contents[a].attrs[e].proCur
            };
            var n = {
                proCur: !0
            };
            for (var i in o.contents[a].attrs) {
                if (-1 != JSON.stringify(o.contents[a].attrs[i]).indexOf(JSON.stringify(n))) {
                    if (this.data.piece >= s) return wx.showToast({
                        title: "该礼包达到兑换数量",
                        icon: "none"
                    }), void (o.contents[a].attrs[e].proCur = !1);
                    o.contents[a].choose = !0;
                    break;
                }
                o.contents[a].choose = !1;
            }
        }
        this.setData({
            acpackage: o
        }), this.piece();
    },
    piece: function() {
        var t = 0, a = this.data.pitch, e = this.data.acpackage, o = this.data.nav[this.data.navCur].id;
        for (var s in e.contents) 1 == e.contents[s].choose && (a = {
            id: o,
            name: this.data.nav[this.data.navCur].name,
            chosepro: e.contents
        });
        for (var n in a.chosepro) for (var i in a.chosepro[n].attrs) a.chosepro[n].choose && 1 == a.chosepro[n].attrs[i].proCur && (t += 1);
        this.setData({
            piece: t,
            pitch: a
        });
    },
    toindex: function() {
        wx.reLaunch({
            url: "/xc_xinguwu/pages/index/index"
        });
    },
    to_det: function(t) {
        var e = this, a = t.currentTarget.dataset.det, o = this.data.acpackage.contents[a];
        app.util.request({
            url: "entry/wxapp/package",
            showLoading: !1,
            data: {
                op: "goodsinfo",
                goodid: a
            },
            success: function(t) {
                var a = t.data.data;
                a.name = o.name, a.price = o.price, a.sale = o.sale, a.attrs = o.attrs, a.weight = o.weight, 
                e.setData({
                    goodsinfo: a
                });
            }
        }), this.setData({
            goods_hide: !1
        });
    },
    det_off: function() {
        this.setData({
            goods_hide: !0
        });
    },
    choose_attrs: function(t) {
        var a = t.currentTarget.dataset.attr, e = this.data.goodsinfo.id, o = this.data.goodsinfo, s = this.data.acpackage;
        s.contents[e].attrs[a].proCur ? (o.attrs[a] = {
            proCur: !1
        }, s.contents[e].attrs[a].proCur = !1) : (o.attrs[a] = {
            proCur: !1
        }, s.contents[e].attrs[a].proCur = !0);
        var n = this.data.chosenum, i = {
            proCur: !0
        };
        for (var c in s.contents[e].attrs) {
            if (-1 != JSON.stringify(s.contents[e].attrs[c]).indexOf(JSON.stringify(i))) {
                if (this.data.piece >= n) return wx.showToast({
                    title: "该礼包达到兑换数量",
                    icon: "none"
                }), void (s.contents[e].attrs[a].proCur = !1);
                s.contents[e].choose = !0;
                break;
            }
            s.contents[e].choose = !1;
        }
        this.setData({
            goodsinfo: o,
            acpackage: s
        }), this.piece();
    },
    addToCar: function() {
        this.setData({
            selectHid: !this.data.selectHid
        });
    },
    remove: function(t) {
        var a = t.currentTarget.dataset.list, e = this.data.acpackage;
        if (e.contents[a]) for (var o in e.contents[a].choose = !1, e.contents[a].attrs) e.contents[a].attrs[o].proCur = !1;
        this.setData({
            acpackage: e
        }), this.piece();
    },
    select_pro: function(t) {
        var a = t.currentTarget.dataset.list, e = t.currentTarget.dataset.attr, o = this.data.acpackage, s = this.data.pitch;
        s.chosepro[a].attrs[e] = {
            proCur: !s.chosepro[a].attrs[e].proCur
        };
        var n = this.data.chosenum, i = {
            proCur: !0
        };
        for (var c in o.contents[a].attrs) {
            if (-1 != JSON.stringify(o.contents[a].attrs[c]).indexOf(JSON.stringify(i))) {
                if (this.data.piece >= n) return wx.showToast({
                    title: "该礼包达到兑换数量",
                    icon: "none"
                }), void (o.contents[a].attrs[e].proCur = !1);
                o.contents[a].choose = !0;
                break;
            }
            o.contents[a].choose = !1;
        }
        this.setData({
            acpackage: o
        }), console.log(this.data.acpackage), this.piece();
    },
    click_empty: function() {
        var e = this, a = this.data.nav[this.data.navCur].id;
        wx.showModal({
            title: "清空提醒",
            content: "确定要清空当前已选商品吗？",
            success: function(t) {
                t.confirm ? app.util.request({
                    url: "entry/wxapp/package",
                    showLoading: !1,
                    data: {
                        op: "packinfo",
                        packageid: a
                    },
                    success: function(t) {
                        var a = t.data.data;
                        e.setData({
                            acpackage: a,
                            pitch: [],
                            piece: 0
                        });
                    }
                }) : t.cancel && console.log("用户点击取消");
            }
        });
    },
    to_buy: function() {
        var t = this.data.pitch, a = {
            id: t.id,
            name: t.name,
            chosepro: []
        };
        for (var e in t.chosepro) t.chosepro[e].choose && (a.chosepro[e] = {
            goodid: parseInt(t.chosepro[e].id),
            name: t.chosepro[e].name,
            bimg: t.chosepro[e].bimg,
            price: t.chosepro[e].price,
            attrs: [],
            weight: t.chosepro[e].weight
        });
        for (var o in a.chosepro) for (var s in t.chosepro[o].attrs) {
            var n = JSON.parse(JSON.stringify(s));
            t.chosepro[o].attrs[n].proCur && a.chosepro[o].attrs.push(s);
        }
        var i = this.data.piece, c = this.data.chosenum;
        if (0 == i) wx.showToast({
            title: "请先选择商品",
            icon: "none"
        }); else if (i < c) {
            var r = c - i;
            wx.showToast({
                title: "还有" + r + "件可选哦~",
                icon: "none"
            });
        } else {
            var p = a.chosepro, d = [];
            for (var o in p) d.push(JSON.parse(JSON.stringify(p[o])));
            console.log(d), a.chosepro = d, app.globalData.packagedata = a, wx.navigateTo({
                url: "../../pages/submitpackage/submit"
            });
        }
        console.log(a);
    },
    onLoad: function(t) {
        var i, c = this;
        this.data.navCur;
        0 <= t.id && (i = t.id), console.log(i), app.util.request({
            url: "entry/wxapp/package",
            showLoading: !1,
            data: {
                op: "index",
                packageid: i
            },
            success: function(t) {
                var a, e, o = t.data.data, s = 0;
                for (var n in o.packagemodellist) o.packagemodellist[n].id == i ? a = n : o.packagemodellist[n].id == o.acpackage.id && (a = n), 
                s += parseInt(o.packagemodellist[n].times);
                e = 0 < o.packagemodellist[n].times ? o.packagemodellist[n].chosenum : 0, app.globalData.packageconfig = o.packagecofnig, 
                c.setData({
                    toalpack: s,
                    nav: o.packagemodellist,
                    acpackage: o.acpackage,
                    navCur: a,
                    chosenum: e,
                    readme: app.globalData.packageconfig.remark
                });
            }
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});